// pages/vip/login/login.js
import { api, Countdown } from '../../../utils/config'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      phone: '',
      codeValue: '',
      showClear: false,
      btnstatus: true,
      smsBtnstatus: false,
      smsText: '获取验证码',
      countdown: 60,
      userInfo: {
        mobile: '',
        regMsgCode: '',
        pwd: '0',
        source: 13,
        openId: '',
        areaId: '',
        from: 0,
        token: '',
        loginType: '',
      }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let openId = 'userInfo.openId'
    let areaId = 'userInfo.areaId'
    let token = 'userInfo.token'
    let loginType = 'userInfo.loginType'
    wx.setNavigationBarTitle({
      title: '绑定手机号',
    })
    wx.getStorage({
      key: 'loginParams', //这里的引号key是缓存里的数值名称
      success: function (res) {
        //console.log(res.data.openId, res.data.areaId, res.data.token, res.data.loginType) //这里的引号res.data是缓存里的数值
        that.setData({ [openId]: res.data.openId || '', [areaId]: res.data.areaId || 9, [token]: res.data.token, [loginType]: res.data.loginType })
  },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({ phone: '', codeValue: '' })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // 判断输入是否为空
  phonenumber:function(e) {
    // console.log(e)
    let that = this
    let mobile = 'userInfo.mobile'
    that.setData({ [mobile]: e.detail.value })
    that.setData({ showClear: true })
    if (this.data.userInfo.mobile && this.data.userInfo.regMsgCode) {
      this.setData({ btnstatus: false })
    }
  },
  sendcode:function(e) {
    let that = this
    let regMsgCode = 'userInfo.regMsgCode'
    that.setData({ [regMsgCode]: e.detail.value })
    this.setData({ showClear: false })
    if (this.data.userInfo.mobile && this.data.userInfo.regMsgCode){
      this.setData({ btnstatus: false })
    }
  },
  //是否手机号为空    
  hasNumber: function() {
    if (this.data.userInfo.mobile.length==0) {
      this.setData({ showClear: false })
    }
    if (this.data.userInfo.mobile != '') {

      // this.setData({ showClear: false })
      if (this.data.userInfo.regMsgCode != '') {
        this.setData({ btnstatus: false })
      }
    } else {
      this.setData({ btnstatus: true })
    }
  },
  //是否验证码为空  
  hasCode: function() {
    if (this.data.userInfo.regMsgCode != '') {
      if (this.data.userInfo.mobile != '') {
        this.setData({ btnstatus: false })
      }
    } else {
      this.setData({ btnstatus: true })
    }
  },
  //清除手机号输入
  clearInput: function (e) {
    // this.setData({ showClear: false })
    this.setData({ phone: '' })
  },
  //获取验证码
  getCode: function (e) {
    let that = this
    if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(this.data.userInfo.mobile)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    // console.log(e)
    let data = {
      mobile: that.data.userInfo.mobile,
      smsType: 2
    }
    console.log(data)
    const url = `${api}/api/sms/yzmMember`
    app.httpClient.post(url, data).then(res => {
      console.log(res)
      if (res.data.code == 200) {
        // let smsBtnstatus = that.data.smsBtnstatus
        // let text = that.data.text
        let countdown = that.data.countdown
        Countdown(that, countdown)
        wx.showToast({
          title: "验证码发送成功",
          icon: 'success',
          duration: 2000,
        })
      }
      else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
  //提交
  submit: function() {
    const url = `${api}/api/bind`      
    if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(this.data.userInfo.mobile)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    if (this.data.userInfo.regMsgCode == '') {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    app.httpClient.post(url, this.data.userInfo).then(res => {
      console.log(res)
      if (res.data.code == 200) {
        wx.setStorage({
          key: 'isBind',
          data: 'true',
        })
        wx.showToast({
          title: "绑定成功",
          icon: 'success',
          duration: 2000,
        })
        //从跳转过来页面传入路径，然后跳回去
        if (this.redirect != undefined) {
          wx.navigateTo({
            url: this.redirect,
          })
        }
        else {
          wx.navigateTo({
            url: 'pages/vip/vip',
          })
        }
      }
      else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  }
})
